package com.itheima.googleplay9.holder;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itheima.googleplay9.R;
import com.itheima.googleplay9.api.Url;
import com.itheima.googleplay9.bean.AppInfo;
import com.itheima.googleplay9.bean.SafeInfo;
import com.itheima.googleplay9.global.GooglePlayApp;
import com.itheima.googleplay9.global.ImageLoaderOptions;
import com.itheima.googleplay9.util.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by lxj on 2016/5/27.
 */
public class AppSafeHolder extends BaseHolder<AppInfo> implements View.OnClickListener{
    @Bind(R.id.iv_image1)
    ImageView ivImage1;
    @Bind(R.id.iv_image2)
    ImageView ivImage2;
    @Bind(R.id.iv_image3)
    ImageView ivImage3;
    @Bind(R.id.iv_safe_arrow)
    ImageView ivSafeArrow;
    @Bind(R.id.iv_safe_icon1)
    ImageView ivSafeIcon1;
    @Bind(R.id.tv_safe_des1)
    TextView tvSafeDes1;
    @Bind(R.id.iv_safe_icon2)
    ImageView ivSafeIcon2;
    @Bind(R.id.tv_safe_des2)
    TextView tvSafeDes2;
    @Bind(R.id.iv_safe_icon3)
    ImageView ivSafeIcon3;
    @Bind(R.id.tv_safe_des3)
    TextView tvSafeDes3;

    @Bind(R.id.ll_safe2)
    LinearLayout ll_safe2;
    @Bind(R.id.ll_safe3)
    LinearLayout ll_safe3;
    @Bind(R.id.ll_container)
    LinearLayout ll_container;

    @Override
    protected View initHolderView() {
        View view = View.inflate(GooglePlayApp.context, R.layout.layout_detail_app_safe, null);

        view.setOnClickListener(this);
        return view;
    }

    @Override
    public void bindData(AppInfo appInfo) {
        ArrayList<SafeInfo> safe = appInfo.getSafe();
        
        //显示第一个
        SafeInfo info0 = safe.get(0);
        ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+info0.getSafeUrl(),ivImage1, ImageLoaderOptions.options);
        ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+info0.getSafeDesUrl(),ivSafeIcon1, ImageLoaderOptions.options);
        tvSafeDes1.setText(info0.getSafeDes());

        //显示第2个,需要判断是否有第2个
        if(safe.size()>1){
            SafeInfo info1 = safe.get(1);
            ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+info1.getSafeUrl(),ivImage2, ImageLoaderOptions.options);
            ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+info1.getSafeDesUrl(),ivSafeIcon2, ImageLoaderOptions.options);
            tvSafeDes2.setText(info1.getSafeDes());
        }else {
            //隐藏第2个
            ll_safe2.setVisibility(View.GONE);
        }


        //显示第3个，也需要判断
        if(safe.size()>2){
            SafeInfo info2 = safe.get(2);
            ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+info2.getSafeUrl(),ivImage3, ImageLoaderOptions.options);
            ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+info2.getSafeDesUrl(),ivSafeIcon3, ImageLoaderOptions.options);
            tvSafeDes3.setText(info2.getSafeDes());
        }else {
            //隐藏第3个
            ll_safe3.setVisibility(View.GONE);
        }


        //一开始应该先使用负的paddingTop的方式隐藏ll_container
        //给ll_container添加全局的布局监听
        ll_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            /**
             * 当ll_container在父View中执行完layout之后调用，
             * 既然布局方法都执行完了，那么宽高是一定可以获取到的
             */
            @Override
            public void onGlobalLayout() {
                //一般该监听器用完之后，立即移除
                ll_container.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                height = ll_container.getHeight();
                ll_container.setPadding(ll_container.getPaddingLeft(),-height,ll_container.getPaddingRight(),
                        ll_container.getPaddingBottom());
//
            }
        });

    }
    private int height;
    private boolean isExpand = false;//是否是展开的
    private boolean isAniming = false;//是否正在执行动画
    @Override
    public void onClick(View v) {
        if(isAniming){
            //动画还没有结束
            return;
        }

        //点击的时候执行伸展动画
        //1.使用ValueAnimator实现自定义动画
        ValueAnimator animator = null;
        if(isExpand){
            //执行收缩动画
            animator = ValueAnimator.ofInt(0,-height);
        }else {
            //执行展开动画
            animator = ValueAnimator.ofInt(-height,0);
        }
        //2.ValueAnimator只是值的改变的动画器，并不会帮你执行任何的动画行为，我们应该监听
        //动画进度更新的过程，根据动画的值实现自己的动画逻辑
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator anim) {
                //1.获取动画的更新的值
                int animatedValue = (int) anim.getAnimatedValue();
                //-87 -86  ... -1   0
                //2.将动画的值设置为当前的paddingTop
                ll_container.setPadding(ll_container.getPaddingLeft(),animatedValue,ll_container.getPaddingRight(),
                        ll_container.getPaddingBottom());
            }
        });
        animator.setDuration(400);
        animator.start();//开启动画

        //3.更改标记值
        isExpand = !isExpand;

        //4.让箭头执行旋转动画
        ViewCompat.animate(ivSafeArrow).rotationBy(180)
                .setListener(new MyAnimListener())
                .setDuration(400).start();
//        ViewCompat.animate(ivImage1).rotationXBy(180).setDuration(400).start();
//        ViewCompat.animate(ivImage2).rotationYBy(180).setDuration(400).start();

    }

    class MyAnimListener implements ViewPropertyAnimatorListener{
        @Override
        public void onAnimationStart(View view) {
            isAniming = true;
        }
        @Override
        public void onAnimationEnd(View view) {
            isAniming =false;
        }
        @Override
        public void onAnimationCancel(View view) {
        }
    }
}
